package com.sjx.boundarycallback.oldrefresh;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * Created by sjx on 2022/7/26
 * 自定义下拉刷新控件
 */
public class RefreshLayout extends LinearLayout {

    private LinearLayout headView;

    private SelfHeadViewManager mSelfHeadViewManager;

    private int minSelfHeadViewPaddingTop;

    /* 头部视图超出最大范围的系数 */
    public static final float MAX_SELF_HEAD_VIEW_PADDING_TOP = 0.3f;

    /* 头部视图最大上边距=头部视图的高度*头部视图超出最大范围的系数，也就是下拉头部显示高度的最大值 */
    private int maxSelfHeadViewHeight;

    public RefreshLayout(Context context) {
        this(context, null);
    }

    public RefreshLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public RefreshLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setOrientation(LinearLayout.VERTICAL);
        init();
    }

    private void init() {
        /* 初始化头部视图 */
        initHeadView();
    }

    /**
     * 设置头部视图管理器
     */
    public void setSelfHeadViewManager(SelfHeadViewManager selfHeadViewManager) {
        this.mSelfHeadViewManager = selfHeadViewManager;
        initSelfHeadView();
    }

    /**
     * 动态添加一个头部的根布局
     */
    private void initHeadView() {
        headView = new LinearLayout(getContext());
        headView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        headView.setBackgroundColor(Color.parseColor("#FF4081"));
        addView(headView);
    }

    /**
     * 初始化具体的头部内容
     */
    private void initSelfHeadView() {
        View selfHeadView = mSelfHeadViewManager.getSelfHeadView();
        int selfHeadViewHeight = mSelfHeadViewManager.getSelfHeadViewHeight();
        minSelfHeadViewPaddingTop = -selfHeadViewHeight;
        /* 最大边界定义为头部高度的0.3 */
        maxSelfHeadViewHeight = (int) (selfHeadViewHeight * MAX_SELF_HEAD_VIEW_PADDING_TOP);
        headView.setPadding(0, maxSelfHeadViewHeight, 0, 0);
        headView.addView(selfHeadView);
    }


}
